<?php
namespace Admin\Controller;
use Think\Controller;
class ManagerController extends CommonController
{	
	//管理员管理
	public function showlist()
	{	

		

		//->desc('create_time')
		$manager = M('manage as a');

		$field = ['a.id','b.role_name'];

		$mage_role = $manager->field($field)->join('rl_mage_role as c on c.mage_id = a.id')->join('rl_role as b on c.role_id = b.role_id')->select();

		$list = $manager->page($_GET['p'],4)->select();

		$count = $manager->count();
		$page = new \Think\Page($count,4);
		$show = $page->show();


		$this->assign('mage_role',$mage_role);
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}

	public function append()
	{
		$info = ['code' => 0 , 'info' => '' , 'status' => 'error'];
		if(IS_POST){
			// $data['uesename'] = I('username','','htmlspecialchars');
			// $data['nickname'] = I('nickname','','htmlspecialchars');
			// $data['password'] = crypt(I('password','','htmlspecialchars'),md5(I('password','','htmlspecialchars'))) ;
			// $data['phone']= I('phone');
			// $data['email'] = I('email');
			// $data['gender'] = I('gender');
		

			
			

			$manage = D('manage');
			

			
			// $manage ->data($data)->add();
		
			if (!$manage->create()){
			     // 如果创建失败 表示验证没有通过 输出错误提示信息
			    $info['code'] = '0';
				$info['info'] = $manage->getError();
				$info['status'] = 'error';
				$this->ajaxReturn($info);
			}else{
				$manage ->add();
				$info['code'] = '1';
				$info['info'] = '添加成功';
				$info['status'] = 'success';
				$this->ajaxReturn($info);
			}

			// $this->ajaxReturn(1);
		}
		$this->display();
	}

	public function edit()
	{
		$info = ['code' => 0 , 'info' => '修改失败' , 'status' => 'error'];
		$manager = M('manage');
		$data = $manager->where(array('id'=>$_GET['id']))->find();
		$this->assign('data',$data);


		if(IS_AJAX){

			//若数据不为空,获取数据

			
			$upd = I('post.');

			//更新数据
			$manage = D('manage');

			// var_dump(I('post.'));die;
			// var_dump($manager->getLastSql());
			if(!$manage->create($upd,2)){
				$info['info'] = $manage->getError();
				$this->ajaxReturn($info);
			}else{
				$updata = $manage->where(array('id' => I('get.id')))->save();
				
				if($updata){
					$info['code'] = 1;
					$info['info'] = '修改成功';
					$info['status'] = 'success';
					$this->ajaxReturn($info);
				}else{
					$info['info'] = $manage->getError()!=''?$manage->getError():'修改失败';
					$this->ajaxReturn($info);
				}
			}

		}

		$this->display();
	}

	public function edit_role(){

		$mage_id = $_GET['id'];

		$role = M('role');
		$manager = M('manage as a');

		$field = ['a.id','b.role_name','c.mage_role_id'];

		$roles = $role->select();

		$mage_role = $manager->field($field)->join('rl_mage_role as c on c.mage_id = a.id')->join('rl_role as b on c.role_id = b.role_id')->where(['c.mage_id'=>$mage_id])->select();

		if(IS_POST){
			$info = ['code' => 0 , 'info' => '' , 'status' => 'error'];
			if($_POST['status'] == 1){

				$mage_role = M('mage_role');
				if(!$mage_role->create()){
					$info['info'] =$mage_role->getError();
					$this->ajaxReturn($info);
				}else{
					$data['mage_id'] = I('get.mage_id');
					$data['role_id'] = I('post.role_id');

					$result = $mage_role->add($data);
					if($result){
						$info['code'] = 1 ;
						$info['info'] = '添加成功';
						$info['status'] = 'success';
						$this->ajaxReturn($info);
					}else{
						$info['info'] = $mage_role->getError();
						$this->ajaxReturn($info);
					}
				}
			}elseif($_POST['status'] == 2){
				$mage_role = M('mage_role');
				$del = $mage_role->where(['mage_role_id' => I("post.mage_role_id")])->delete();
				if($del){
					$info['code'] = 1 ;
					$info['info'] = '删除成功';
					$info['status'] = 'success';
					$this->ajaxReturn($info);
				}else{
					$info['info'] = $mage_role->getError();
					$this->ajaxReturn($info);
				}
			}
		}

		$this->assign('mage_id',$mage_id);
		$this->assign('mage_role',$mage_role);
		$this->assign('roles',$roles);
		$this->display();
	}

	public function del()
	{	
		if(IS_AJAX){

			$manage = M('manage');

			$del = $manage->where(array('id'=>$_GET['id']))->save(['status'=>0]);

			if($del){
				$info['code'] = 1;
				$info['info'] = '已冻结';
				$info['status'] = 'success';
				$this->ajaxReturn($info);

			}else{
				$info['code'] = 0;
				$info['info'] = '冻结失败';
				$info['status'] ='error';
				$this->ajaxReturn($info);
			}

		}
	}

	
}